package com.demo.logistics.stockControl.presenter;

import java.util.ArrayList;

import android.app.Activity;

import com.demo.logistics.entities.Products;
import com.demo.logistics.entities.Stocks;
import com.demo.logistics.stockControl.INewProductView;
import com.demo.logistics.stockControl.IProductListView;
import com.demo.logistics.stockControl.IStockListView;
import com.demo.logistics.stockControl.IStockReceiveView;
import com.demo.logistics.stockControl.data.AllDataModel;
import com.demo.logistics.stockControl.ui.StockListArrayAdapter;
import com.demo.logistics.utils.DefaultValidator;

public class StockPresenter {
	private IStockReceiveView _view;
	private IStockListView _view2;
	private INewProductView _view3;
	private IProductListView _view4;
	private AllDataModel _model;
	private DefaultValidator val;
	private String errorMsg;
	
	public String getErrorMsg() {
		return errorMsg;
	}

	public StockPresenter(IStockReceiveView view){
		val = new DefaultValidator();
		_view = view;
		_model = AllDataModel.getInstance();
	}
	
	public StockPresenter(IStockListView view){
		val = new DefaultValidator();
		_view2 = view;
		_model = AllDataModel.getInstance();
	}
	
	public StockPresenter(INewProductView view){
		val = new DefaultValidator();
		_view3 = view;
		_model = AllDataModel.getInstance();
	}
	
	public StockPresenter(IProductListView view){
		val = new DefaultValidator();
		_view4 = view;
		_model = AllDataModel.getInstance();
	}
	
	public boolean handleStockSubmitBtn(String prod, String qty, String lorryNo, String driverName){
		//TODO get all input and write to database
		//boolean prodOk, qtyOk, lorryNoOk, driverNameOk = true;
		boolean allOk = true;
		errorMsg = "";
		if(prod==null || prod.length()==0){
			errorMsg += "Product is empty\n";
			allOk = false;
		}
		if(qty==null || qty.length()==0){
			errorMsg += "Quantity is empty\n";
			allOk = false;
		}
		else{
			if(val.containsOnlyNumbers(qty)==false){
				errorMsg += val.getValidationMsg()+"\n";
				allOk = false;
			}
		}
		if(lorryNo==null || lorryNo.length()==0){
			errorMsg += "Lorry No. is empty\n";
			allOk = false;
		}
		if(driverName==null || driverName.length()==0){
			errorMsg += "Driver name is empty\n";
			allOk = false;
		}
		if(allOk==true){
			Stocks newStock = new Stocks(prod, Integer.parseInt(qty), lorryNo, driverName, null);
			_model.addNewStock(newStock);
			return true;
		}
		else{
			
			return false;
		}
	}
	
	public boolean handleProductSubmitBtn(String name, String brand, String price){
		boolean allOk = true;
		errorMsg = "";
		if(name==null || name.length() == 0){
			errorMsg += "Product is empty\n";
			allOk = false;
		}
		if(brand==null || brand.length() == 0){
			errorMsg += "Brand is empty\n";
			allOk = false;
		}
		if(price==null || price.length() == 0){
			errorMsg += "Price is empty\n";
			allOk = false;
		}
		else{
			if(val.validatePrice(price)==false){
				errorMsg += "Price is not in 88.88 format\n";
				allOk = false;
			}
		}
		if(allOk==true){
			Products newProd = new Products(name, brand, Double.parseDouble(price));
			if(_model!=null){
				_model.addNewProduct(newProd);
			}
			else{
				System.out.println("***model is null***");
			}
			
			return true;
		}
		else{
			return false;
		}
	}
	
	public void handleProductSelection(String item){
		if(item!=null){
			if(item.length() > 0){
				
			}
		}
	}
	
	public ArrayList<Stocks> fetchAllStocks(){
		ArrayList<Stocks> allStocks = _model.getAllStocks();
		if(allStocks.size() > 0){
			return allStocks;
		}
		return null;
	}
	
	public ArrayList<Products> fetchAllProducts(){
		ArrayList<Products> allProducts = _model.getAllProducts();
		if(allProducts.size() > 0){
			return allProducts;
		}
		return null;
	}
	
	public String[] fetchAllProductNames(){
		ArrayList<Products> allProducts = _model.getAllProducts();
		int len = allProducts.size();
		if(len > 0){
			String[] prodNames = new String[len];
			for(int i=0; i<len; i++){
				prodNames[i] = allProducts.get(i).getName();
			}
			return prodNames;
		}
		return null;
	}
}
